<?php
if( !defined('DB_NAME')) die();
if( get('ini_login') ) add_action( 'login_head', 'ini_login_logo', 200);
// 登录界面LOGO
function ini_login_logo(){
	$logo= '<style type="text/css">h1 a{background-image:url('.get('ini_login').')!important;}</style>';
	echo $logo;
}

add_action( 'login_head', create_function(false,'echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"'.get_template_directory_uri().'/res/css/hack.css\" ></link>";') );
add_filter( 'login_headerurl', create_function(false,"return get_bloginfo('url');") );
add_filter( 'login_headertitle', create_function(false,"return get_bloginfo('name');") );

// 登录界面背景
function ini_login_bg(){
	$canvas = '<img id="gbk" src="'.TPDIR.'/res/img/big'.rand(1,6).'.jpg" />';
	$canvas.= '<canvas id="gb"></canvas>';
	$canvas.= '<script src="'.TPDIR.'/res/js/StackBlur.js"></script>';
	$canvas.= '<script>document.getElementById("gbk")&&setTimeout(function(){stackBlurImage( "gbk", "gb", 30 )},10);</script>';
	echo $canvas;
}
add_action('login_form', 'ini_login_bg', 100);
add_action('register_form', 'ini_login_bg', 100);
add_action('lostpassword_form', 'ini_login_bg', 100);

// 添加登录欢迎语
function ini_login_message( $message ){
	if ( empty($message) ){
		return "<p class=\"message\">欢迎登录本站点！</p>";
	} else {
		return $message;
	}
}
add_filter( 'login_message', 'ini_login_message' );

// 仿恶意登录
//add_filter('login_errors', create_function('$a', 'return "用户或密码不正确！";'));



################################################################
#  后台注册模块，添加注册表单，修改新用户通知，来自 Ludou.org  #
################################################################

if ( !function_exists('wp_new_user_notification') ) :

function wp_new_user_notification($user_id, $plaintext_pass = '', $flag=''){

	if( func_num_args() > 1 && $flag !== 1 )
		return;

	$user = new WP_User($user_id);
	$user_login = stripslashes($user->user_login);
	$user_email = stripslashes($user->user_email);

	// 发送给管理员的注册通知邮件
	$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
	$message = sprintf(__('亲，您的站点 [ %s ] 有新用户注册！'), $blogname)."\r\n\r\n";
	$message.= sprintf(__('用户名称：%s'), $user_login)."\r\n";
	$message.= sprintf(__('电子邮件：%s'), $user_email)."\r\n";
	wp_mail(get_option('admin_email'), sprintf(__('[ %s ] 新用户注册'), $blogname), $message);

	if ( empty($plaintext_pass) )
		return;

	// 发送给新用户的注册通知邮件
	$message = sprintf(__('用户名称：%s'), $user_login)."\r\n";
	$message.= sprintf(__('登录密码：%s'), $plaintext_pass)."\r\n";
	$message.= '登陆网址: '.wp_login_url()."\r\n";
	wp_mail($user_email, sprintf(__('[ %s ] 用户注册信息'), $blogname), $message);
}
	endif;

// 添加注册表单
add_action('register_form','ini_password');
function ini_password(){
	define('LCR_PLUGIN_URL', plugin_dir_url( __FILE__ ));
?>
<p>
	<label for="user_pwd">用户密码<br/>
		<input id="user_pwd" class="input" type="password" size="25" value="<?php echo $_POST['user_pass']; ?>" name="user_pass"/>
	</label>
</p>
<p>
	<label for="user_pwd_cmf">确认密码<br/>
		<input id="user_pwd_cmf" class="input" type="password" size="25" value="<?php echo $_POST['user_pass2']; ?>" name="user_pass2" />
	</label>
</p>
<p>
	<label for="user_role">用户身份:
		<select name="user_role" id="user_role" style="padding:3px 6px;">
			<option value="subscriber" <?php if($_POST['user_role'] == 'subscriber') echo 'selected="selected"';?>>订阅者</option>
			<option value="contributor" <?php if($_POST['user_role'] == 'contributor') echo 'selected="selected"';?>>投稿者</option>
		</select>
	</label>
	<br />
</p>
<?php
}

// 表单数据处理
function ini_check($login, $email, $errors){
	global $wpdb;
	$last_reg = $wpdb->get_var("SELECT `user_registered` FROM `$wpdb->users` ORDER BY `user_registered` DESC LIMIT 1");

	if( (time() - strtotime($last_reg)) < 60 )  # 防恶意注册，60s
		$errors->add('anti_spam', "<strong>错误</strong>：先歇会，稍后再注册，谢谢您的理解");

	if( strlen($_POST['user_pass']) < 6 )
		$errors->add('password_length', "<strong>错误</strong>：密码长度至少6位");
	elseif($_POST['user_pass'] != $_POST['user_pass2'])
		$errors->add('password_error', "<strong>错误</strong>：两次输入的密码必须一致");

	if($_POST['user_role'] != 'contributor' && $_POST['user_role'] != 'subscriber')
		$errors->add('role_error', "<strong>错误</strong>：不存在的用户身份");
}

// 保存表单数据
function ini_register_data($user_id, $password="", $meta=array()){
	$userdata = array();
	$userdata['ID'] = $user_id;
	$userdata['user_pass'] = $_POST['user_pass'];
	$userdata['role'] = $_POST['user_role'];
	wp_new_user_notification( $user_id, $_POST['user_pass'], 1 );
	wp_update_user($userdata);
}
if(function_exists('themedie')):
function ini_remove_password_nag(){
	global $user_ID;
	delete_user_setting('default_password_nag', $user_ID);
	update_user_option($user_ID, 'default_password_nag', false, true);
}

function ini_reg_text( $translated_text, $untranslated_text ){
	if ( $untranslated_text === 'A password will be e-mailed to you.' )
		return '';
	else if ($untranslated_text === 'Registration complete. Please check your e-mail.')
		return '注册成功！';
	else
		return $translated_text;
}

add_action('register_post','ini_check',10,3);
add_action('user_register', 'ini_register_data');
add_action('admin_init', 'ini_remove_password_nag');
add_filter('gettext', 'ini_reg_text', 20, 3);

endif;
?>